package com.ss.android.newmedia.message;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.bytedance.common.plugin.interfaces.pushmanager.message.MessageReceiverService;
import com.bytedance.common.utility.Logger;
import com.ss.android.common.app.AppHooks.InitHook;
import com.ss.android.common.app.AppHooks;
import com.ss.android.newmedia.BaseAppData;
import java.util.Iterator;
import java.util.List;
import org.json.JSONObject;

public class MessageHandler extends MessageReceiverService {
    public static final Uri a;

    static {
        a = Uri.parse("content://com.ss.android.newmedia.message/messages");
    }

    public MessageHandler() {

    }

    public void onCreate() {
        try {
            super.onCreate();
            InitHook v0_1 = AppHooks.getInitHook();
            if(v0_1 != null) {
                v0_1.tryInit(((Context)this));
            }

            b.a(((Context)this));
        }
        catch(Throwable v0) {
        }
    }

    public void onHandMessage(Context arg11, int arg12, String arg13, int arg14, String arg15) {
        BaseAppData v2 = BaseAppData.inst();
        com.ss.android.newmedia.message.b v1 = com.ss.android.newmedia.message.b.a(this);
        if(!com.ss.android.newmedia.message.c.a().e()) {
            v1.a.a();
            i.a(arg11, arg13, v2, arg14, arg15, false);
            return;
        }

        if(!android.support.design.a.isTextEmpty(arg13)) {
            try {
                JSONObject v0_1 = new JSONObject(arg13);
                int v3 = v0_1.optInt("id", 0);
                if(v3 <= 0) {
                    i.a(arg11, arg13, v2, arg14, arg15, false);
                    return;
                }

                if(v0_1.optInt("pass_through", 1) <= 0) {
                    i.a(arg11, arg13, v2, arg14, arg15, false);
                    return;
                }

                com.ss.android.newmedia.message.b.a v0_2 = new com.ss.android.newmedia.message.b.a();
                v0_2.a = v3;
                v0_2.b = 1;
                v0_2.c = arg13;
                v0_2.d = 2;
                v0_2.e = null;
                v0_2.f = System.currentTimeMillis() / 1000;
                v1.a.a(Integer.valueOf(v3), v0_2);
            } catch(Throwable v0) {
                v0.printStackTrace();
            }
            v1.b();
        }
        i.a(arg11, arg13, v2, arg14, arg15, false);
    }

    public void onReceive(Context arg9, Intent arg10) {
        int v1_3 = 0;
        super.onReceive(arg9, arg10);
        if(arg9 != null && arg10 != null) {
            String v1 = arg10.getAction();
            if((Logger.debug()) && v1 != null) {
                Logger.d("MessageHandler", "action = " + v1);
            }

            if(!com.ss.android.newmedia.message.c.a().h()) {
                Logger.i("MessageHandler", "notify enable = " + com.ss.android.newmedia.message.c.a().h());
                return;
            }

            try {
                if("com.ss.android.newmedia.message.cache.action".equals(v1)) {
                    List v1_2 = b.a(((Context)this)).a();
                    if(v1_2 == null) {
                        return;
                    }

                    if(v1_2.isEmpty()) {
                        return;
                    }

                    Iterator v7 = v1_2.iterator();
                    while(true) {
                        if(!v7.hasNext()) {
                            return;
                        }

                        Object v5 = v7.next();
                        if(v5 == null) {
                            continue;
                        }

                        if(Logger.debug()) {
                            Logger.d("MessageHandler", "cache messageObj = " + ((com.ss.android.newmedia.message.b.a)v5).toString());
                        }

                        i.a(this.getApplicationContext(),
                                ((com.ss.android.newmedia.message.b.a)v5).c,
                                BaseAppData.inst(),
                                ((com.ss.android.newmedia.message.b.a)v5).d,
                                ((com.ss.android.newmedia.message.b.a)v5).e,
                                true);
                    }
                }

                if(!"com.ss.android.newmedia.message.notify.delete.action".equals(v1)) {
                    return;
                }

                v1_3 = arg10.getIntExtra("id", 0);
                if(v1_3 != 0) {
                }else {
                    Uri v3 = arg10.getData();
                    if (v3 == null) {
                    } else {
                        v1 = v3.getLastPathSegment();
                        try {
                            v1_3 = Integer.parseInt(v1);
                            if (!Logger.debug()) {
                            } else {
                                Logger.d("MessageHandler", "delete cache id = " + v1_3);
                            }
                        } catch (NumberFormatException v1_4) {
                            v1_3 = 0;
                        }
                    }
                }
            } catch(Exception v1_1) {
            }

            try {
                b.a(((Context)this)).a(v1_3);
            }
            catch(Exception v1_1) {
                v1_1.printStackTrace();
            }
        }
    }

    public int onStartCommand(Intent arg2, int arg3, int arg4) {
        super.onStartCommand(arg2, arg3, arg4);
        return Service.START_NOT_STICKY;
    }
}

